package one.stand.interceptor;

import one.stand.enums.ResultEnum;
import one.stand.model.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: QIK
 * @CreateDate: 2019/5/6 10:37
 */
@Component
public class ParamInterceptor extends HandlerInterceptorAdapter {
    protected static final Logger log = LoggerFactory.getLogger(ParamInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
        StringBuffer requestURL = httpServletRequest.getRequestURL();
        log.info("参数前置拦截器1 preHandle： 请求的uri为：" + requestURL.toString());
        try {
            String data = httpServletRequest.getParameter("data");
            log.info("请求参数：{}", data);
            return true;
        } catch (Exception e) {
            ResultModel.returnJson(httpServletResponse, ResultEnum.PARAM_CHECK, e.getMessage());
            return false;
        }
    }
}
